package com.safeluck.scannerqcr.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;

import com.safeluck.scannerqcr.listener.OnTabSelectedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * ScannerQCR
 * Created by lzw on 2017/11/28. 11:46:48
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class BottomBar extends LinearLayout {

    private static final int TRANSLATE_DURATION_MILLIS = 200;//动画时间
    private List<BottomBarTab> mTabs = new ArrayList<>();
    private LinearLayout mTabLayout ;
    private Context mContext;
    private LayoutParams mTabParms;
    private OnTabSelectedListener listener;
    private int mCurrentPosition = 0;
    private boolean mVisible= true;
    private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator();

    public BottomBar(Context context) {

        this(context,null);
    }

    public BottomBar(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public BottomBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    public void setOnTabSelectedListener(OnTabSelectedListener l){
        listener = l;
    }

    private void init(Context context, AttributeSet attrs) {
        setOrientation(VERTICAL);
        this.mContext = context;
        mTabLayout = new LinearLayout(context);
        mTabLayout.setBackgroundColor(Color.WHITE);
        mTabLayout.setOrientation(LinearLayout.HORIZONTAL);
        addView(mTabLayout,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mTabParms = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        mTabParms.weight = 1;
    }

    public BottomBar addItem(final BottomBarTab tab){
        tab.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener == null){
                    return;
                }
                int pos = tab.getTabPosition();
                if (pos == mCurrentPosition){
                    listener.onTabReselected(pos);
                }else{
                    listener.onTabSelected(pos,mCurrentPosition);
                    tab.setSelected(true);
                    listener.onTabUnselected(mCurrentPosition);
                    mTabs.get(mCurrentPosition).setSelected(false);
                    mCurrentPosition = pos;
                }

            }
        });
        tab.setTabPosition(mTabLayout.getChildCount());
        tab.setLayoutParams(mTabParms);
        mTabLayout.addView(tab);
        mTabs.add(tab);
        return this;
    }

    public BottomBarTab getItem(int index){
        if (mTabs.size()>=index){
            return mTabs.get(index);
        }
        return null;
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState= super.onSaveInstanceState();
        return new SavedState(superState,mCurrentPosition);
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        if (mCurrentPosition != ss.position){
            mTabLayout.getChildAt(mCurrentPosition).setSelected(false);
            mTabLayout.getChildAt(ss.position).setSelected(true);
        }
        mCurrentPosition = ss.position;
    }

    static class SavedState extends BaseSavedState{
        private int position;
        public SavedState(Parcel source) {
            super(source);
            position = source.readInt();
        }

        public SavedState(Parcelable source, int position) {
            super(source);
            this.position = position;
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(position);
        }

        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
            @Override
            public SavedState createFromParcel(Parcel parcel) {
                return new SavedState(parcel);
            }

            @Override
            public SavedState[] newArray(int i) {
                return new SavedState[i];
            }
        };
    }


    public void show(){
        show(true);
    }

    private void show(boolean anim) {
        toggle(true,anim,false);
    }

    private void toggle(final boolean visible, final boolean animate, boolean force){
        if (mVisible != visible || force){
            mVisible =  visible;
            int height = getHeight();
            if (height ==0 && !force){
                ViewTreeObserver vto = getViewTreeObserver();
                if (vto.isAlive()){
                    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                        @Override
                        public boolean onPreDraw() {
                            ViewTreeObserver currrentVto= getViewTreeObserver();
                            if (currrentVto.isAlive()){
                                currrentVto.removeOnPreDrawListener(this);
                            }
                            toggle(visible,animate,true);
                            return true;
                        }
                    });
                    return;
                }
            }
            int translationY = visible?0:height;
            if (animate){
                animate().setInterpolator(mInterpolator)
                        .setDuration(TRANSLATE_DURATION_MILLIS)
                        .translationY(translationY);
            }else{
                ViewCompat.setTranslationY(this,translationY);
            }
        }
    }

    public void hide(){
        hide(true);
    }

    private void hide(boolean b) {
        toggle(false,b,false);
    }
}
